package permission

import (
	"context"
	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"product/common/validator"
)

var _ PermissionsModel = (*customPermissionsModel)(nil)

type (
	// PermissionsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPermissionsModel.
	PermissionsModel interface {
		permissionsModel
		withSession(session sqlx.Session) PermissionsModel
		// ModifyPermissions 修改权限
		ModifyPermissions(ctx context.Context, id int64,
			fields map[string]interface{}) (bool, error)
		// FindPermissions 分页查询权限
		FindPermissions(ctx context.Context, page, pageSize int64,
			conditions map[string]interface{}) ([]*Permissions, error)
		// CheckValueExists 检查权限值是否存在
		CheckValueExists(ctx context.Context, value int64) bool
		// CheckNameExists 检查权限名称是否存在
		CheckNameExists(ctx context.Context, name string) bool
	}

	customPermissionsModel struct {
		*defaultPermissionsModel
	}
)

func (m *customPermissionsModel) CheckValueExists(ctx context.Context, value int64) bool {
	sql, args, err := m.genCheckValueExistsSql(value).ToSql()
	if err != nil {
		return false
	}
	var id int64
	err = m.conn.QueryRowCtx(ctx, &id, sql, args...)
	return err == nil && id > 0
}

func (m *customPermissionsModel) CheckNameExists(ctx context.Context, name string) bool {
	sql, args, err := m.genCheckNameExistsSql(name).ToSql()
	if err != nil {
		return false
	}
	var id int64
	err = m.conn.QueryRowCtx(ctx, &id, sql, args...)
	return err == nil && id > 0
}

func (m *customPermissionsModel) ModifyPermissions(ctx context.Context, id int64,
	fields map[string]interface{}) (bool, error) {
	sql, args, err := m.genModifyPermissionsSql(id, fields).ToSql()
	if err != nil {
		return false, err
	}
	_, err = m.conn.ExecCtx(ctx, sql, args...)
	return err == nil, err
}

func (m *customPermissionsModel) FindPermissions(ctx context.Context, page, pageSize int64,
	conditions map[string]interface{}) ([]*Permissions, error) {
	sql, args, err := m.genFindPermissionsSql(page, pageSize, conditions).ToSql()
	if err != nil || validator.IsEmptyString(sql) {
		return nil, err
	}
	var resp []*Permissions
	err = m.conn.QueryRowsCtx(ctx, &resp, sql, args...)
	switch err {
	case nil:
		return resp, nil
	case sqlx.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

// NewPermissionsModel returns a model for the database table.
func NewPermissionsModel(conn sqlx.SqlConn) PermissionsModel {
	return &customPermissionsModel{
		defaultPermissionsModel: newPermissionsModel(conn),
	}
}

func (m *customPermissionsModel) withSession(session sqlx.Session) PermissionsModel {
	return NewPermissionsModel(sqlx.NewSqlConnFromSession(session))
}

// #### 私有方法 ###
func (m *customPermissionsModel) genModifyPermissionsSql(id int64,
	fields map[string]interface{}) squirrel.UpdateBuilder {
	sql := squirrel.Update(m.table).SetMap(fields).Where("ID = ?", id).Where("IsDeleted = 0")
	return sql
}

func (m *customPermissionsModel) genFindPermissionsSql(page, pageSize int64,
	conditions map[string]interface{}) squirrel.SelectBuilder {
	sql := squirrel.Select(permissionsRows).From(m.table).Where("IsDeleted = 0").Where(conditions)
	return sql.Offset(uint64((page - 1) * pageSize)).Limit(uint64(pageSize))
}

func (m *customPermissionsModel) genCheckValueExistsSql(value int64) squirrel.SelectBuilder {
	sql := squirrel.Select("ID").From(m.table).Where("PermissionValue = ?", value).Limit(1)
	return sql
}

func (m *customPermissionsModel) genCheckNameExistsSql(name string) squirrel.SelectBuilder {
	sql := squirrel.Select("ID").From(m.table).Where("PermissionName = ?", name).Limit(1)
	return sql
}
